<?php
namespace app\controller;
use app\model\Recharge;
use app\model\OpAccount;
use app\model\AccRefund;
use think\facade\Db;
use app\model\Items;
class Test {

    public function get_img(){
        $s = md5_file('https://mt-imgs.oss-cn-hangzhou.aliyuncs.com/20210223145844_78341.jpg');
        dump($s);
    
    //   b02eeaa3d8074cc0b3e08593046ccd55 
    }
    // 签名
    function get_signature(){
        
    }

    //
    public function index(){
        // dump('1测试');
        // die();
        // $w[] = ['sell_profit','<',0];
        // $list = Recharge::where($w)->select();
        // foreach($list as $k=>$v){
        //     if($v['currency'] == 0){
        //         $da['amount'] = 0;
        //         $da['channel_money'] = 0;
        //         $da['profit'] = 0;
        //         $da['sell_profit'] = 0;
        //     }else{
        //         $da['amount'] = zhb_cny($v['currency'],$v['rebate']);
        //         $da['channel_money'] = zhb_cny($v['currency'],$v['channel_rebate']);
        //         $da['profit'] = cny_profit($v['currency'],$v['rebate'],$v['channel_rebate']);
        //         if($v['sell_rebates'] > 0 ){
        //             $da['sell_profit'] = cny_profit($v['currency'],$v['rebate'],$v['sell_rebates']);
        //         }else{
        //             $da['sell_profit'] = 0;
        //         }   
        //     }
        //     // $r = Recharge::where('id',$v['id'])->update($da);
        //     // dump($r);
        //     echo '编号：'.$v['code'].',完成更新';
        // }

        $wh[] = ['code','=','ACC2020120999037'];
        $rs = Recharge::where($wh)->find();
        if($rs['currency'] == 0){
            $da['amount'] = 0;
            $da['channel_money'] = 0;
            $da['profit'] = 0;
            $da['sell_profit'] = 0;
        }else{
            $da['amount'] = zhb_cny($rs['currency'],$rs['rebate']);
            $da['channel_money'] = zhb_cny($rs['currency'],$rs['channel_rebate']);
            $da['profit'] = cny_profit($rs['currency'],$rs['rebate'],$rs['channel_rebate']);
            if($rs['sell_rebates'] > 0 ){
                $da['sell_profit'] = cny_profit($rs['currency'],$rs['rebate'],$rs['sell_rebates']);
            }else{
                $da['sell_profit'] = 0;
            }

            
        }

        dump($da);die();
        $r = Recharge::where('id',$rs['id'])->update($da);
        dump($r);
    }

    function save()
    {
        $wh[] = ['status','>',0];
        $list = AccRefund::where($wh)->select();
        foreach($list as $k=>$v){
            $this->get_save($v);
        }

        //dump($list);
    }
    function get_save($v){
        $r = Recharge::where('id',$v['pid'])->find();
        $data['pid'] = $v['pid'];
        $data['type'] = 4;
        if($v['status'] == 2){
            $data['status'] = 1;
        }elseif($v['status'] == 1){
            $data['status'] = 0;
        }elseif($v['status'] == 0){
            $data['status'] = -1;
        }
       
        $data['currency'] = $v['r_currency'];
        $data['amount'] = zhb_cny($v['r_currency'],$r['rebate']);
        $data['rebate'] = $r['rebate'];
        $data['channel_rebate'] = $r['channel_rebate'];
        $data['channel_money'] = zhb_cny($v['r_currency'],$r['channel_rebate']);
        $data['profit'] = cny_profit($v['r_currency'],$r['rebate'],$r['channel_rebate']);
        $data['sell_rebates'] = $r['sell_rebates'];
        $data['sell_profit'] = cny_profit($v['r_currency'],$r['rebate'],$r['sell_rebates']);
        $data['create_time'] = strtotime($v['create_time']);
        $data['update_time'] = strtotime($v['update_time']);
        $data['audit_id'] = $v['audit_id'];
        $data['note'] = $v['note'];
        $data['audit_proof_url'] = $v['refund_url'];
        $data['items_id'] = $r['items_id'];
        $data['op_account_id'] = $r['op_account_id'];
        $data['media_id'] = $r['media_id'];
        $data['customer_id'] = $r['customer_id'];
        $data['channel_id'] = $r['channel_id'];
        $data['code'] = get_numbers('TK');
        $data['screenshot_url'] = $v['media_refund_url'];
        
        try {
            Db::name('recharge')->save($data);
            echo $data['code'].'添加成功<br/>';
        } catch (\Throwable $th) {
            //throw $th;
            echo $th->getMessage().',pid=='.$data['pid'].',添加失败<br/>';
        }
        
    }
    
    function get_ss(){
        $wh[] = ['is_advance','=',1];
        $wh[] = ['type','=',1];
        $wh[] = ['status','=',1];
        //$wh[] = ['customer_id','=',$customer_id];
        $rs = Recharge::where($wh)->select()->toArray();
        dump($rs);
        // foreach($rs as $k=>$v){

        // }
    }
    function get_ss_a($v){
        // 启动事务
        Db::startTrans();
        try {
            Db::name('customer')->where('id', $v['customer_id'])->dec('prepare_money', $v['amount'])->update();
            Db::name('customer')->where('id', $v['customer_id'])->dec('total_recharge', $v['amount'])->update();
            
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }
















}